<script setup>
import { ref } from 'vue'
import { EditCategoryService, AddCategoryService } from '@/api/vegeFru.js'
const dialogVisible = ref(false)
const formRef = ref()
const formModel = ref({
  mt: '',
  st: ''
})
const emit = defineEmits(['success'])

const open = async (row) => {
  dialogVisible.value = true
  formModel.value = { ...row }
  console.log(formModel)
}

defineExpose({
  open
})

const onSubmit = async () => {
  await formRef.value.validate()
  formModel.value.id
    ? await EditCategoryService(formModel.value)
    : await AddCategoryService(formModel.value)
  ElMessage({
    type: 'success',
    message: formModel.value.id ? '编辑成功' : '添加成功'
  })
  dialogVisible.value = false
  emit('success')
}

const rules = {
  mt: [{ required: true, message: '请选择分类名称', trigger: 'blur' }],
  st: [{ required: true, message: '请输入品种', trigger: 'blur' }]
}
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    :title="formModel.id ? '编辑果蔬品种' : '添加果蔬品种'"
    width="30%"
  >
    <el-form
      ref="formRef"
      :model="formModel"
      :rules="rules"
      label-width="100px"
      style="padding-right: 30px"
    >
      <el-form-item label="分类名称" prop="mt">
        <el-select v-model="formModel.mt" style="width: 100%">
          <el-option lable="水果" value="水果"></el-option>
          <el-option lable="蔬菜" value="蔬菜"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="品种" prop="st">
        <el-input v-model="formModel.st" placeholder="请输入品种"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="onSubmit"> 确认 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
